package Snackbarsystem;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class allAccount {
    public void managerAccount() throws Exception {
        Scanner ac = new Scanner(System.in);
        while (true) {
        try (
                BufferedReader out0 = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                BufferedReader out1 = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
        ) {
            System.out.println("请输入账号：");
            String account = ac.next();
            System.out.println("请输入密码：");
            String passWord = ac.next();
            if (account.equals(out0.readLine()) && passWord.equals(out1.readLine())) {
                managerFood m = new managerFood();
                m.show();
                break;
            } else {
                System.out.println("账号或密码错误！");
            }
        }
        }
    }
    public void userAcconut() throws Exception {
        System.out.println("您好亲爱的用户！");
        for (int i = 1; i <= 3; i++) {
            System.out.println();
        }
        System.out.println("1.注册         2.登陆");
        Scanner bc = new Scanner(System.in);
        String ch = bc.next();
        //注册新用户
        if (ch.equals("1")) {
            try (
                    BufferedWriter in0 = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\Account.txt", true));
                    BufferedWriter in1 = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt", true));
            ) {
                Scanner cc = new Scanner(System.in);
                while (true) {
                    System.out.println("账号：（请输入1位数字、字母、符号:）");
                    String newAccount = cc.next();
                    if (newAccount.length() == 1) {
                        in0.write(newAccount + "\n");
                        in0.flush();
                    } else {
                        System.out.println("账号长度输入错误！");
                    }
                    System.out.println("密码：（请输入2位数字、字母、符号:）");
                    String newpassWord = cc.next();
                    if (newpassWord.length() >= 1 && newpassWord.length() <= 12) {
                        in1.write(newpassWord + "\n");
                        in1.flush();
                        System.out.println("注册成功！");
                        break;
                    } else {
                        System.out.println("密码长度错误！");
                    }
                }
                }
            }

        //用户登陆
        else if(ch.equals("2")){
            List<String> j=new ArrayList<String>();//储存判断密码账号是否相等
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入账号：");
            String account=sc.next();
            System.out.println("请输入密码：");
            String passWord=sc.next();
        try(
                BufferedReader out1 = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                BufferedReader out = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                BufferedReader out2=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                ) {
            int n=0;
            while ((out.readLine())!=null){
                n++;//获得现有密码行数
            }
            //将输入的密码与已储存每行密码一一比对
           for (int i=1;i<=n;i++){
               String a = out1.readLine();
               String b = out2.readLine();
                   if (a.equals(account) && b.equals(passWord))
                   {
                       j.add("yes");
                       System.out.println("登陆成功！");
                   userFood u = new userFood();
                   u.user();//进入用户方法
               }
           }
            if ((j.isEmpty())==true){
                System.out.println("账号或密码错误！");
        }
        }
       }
        else {
            System.out.println("功能不存在！");
        }
    }
}
